package com.yz.jdd.voice;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Vibrator;
import android.text.TextUtils;
import android.util.Log;

import com.yz.jdd.R;
import com.yz.jdd.base.utils.BaseConfigs;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class AudioPlayerInstance {
    private Context context;
    private boolean isInit;
    private List<Integer> queue;
    public static final String TAG = "zx";

    private MediaPlayer mediaPlayer;
//    private Vibrator vibrator;
    long[] pattern = {100, 400, 100, 400}; // 停止 开启 停止 开启;

    private static class SingletonHolder {
        private static final AudioPlayerInstance INSTANCE = new AudioPlayerInstance();
    }

    public static final AudioPlayerInstance getInstance(Context context) {

        if (!SingletonHolder.INSTANCE.isInit) {
            SingletonHolder.INSTANCE.init(context);
        }
        return SingletonHolder.INSTANCE;
    }

    public void init(Context context) {
        this.context = context.getApplicationContext();
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setOnCompletionListener(completionListener);
        mediaPlayer.setOnErrorListener(errorListener);
        mediaPlayer.setVolume(1f, 1f);
        queue = new ArrayList();
        isInit = true;
//        vibrator = (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE);
    }

    public void say(int type, String count) {
        int times = 0;//默认播放次数
        if (TextUtils.isEmpty(count)) {
            times = 0;
        } else {
            try {
                times = Integer.parseInt(count);
            } catch (Exception e) {
                times = 0;
            }
        }
        if (times <= 0) {
            return;
        }
        for (int i = 0; i < times; i++) {
            queue.add(type);
        }
        if (!mediaPlayer.isPlaying()) {
            if (queue != null && queue.size() > 0) {
                Uri uri = null;
                int nowType = queue.get(0);
                if (BaseConfigs.VOICE_SCAN_HINT == nowType) { //扫码提示
                    uri = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.scan_hint);
                }else{

                }
                if (uri != null) {
                    //      LogBackUtil.i("播报Uri :" + uri.getPath());
                    try {
//                        vibrator.vibrate(pattern, -1);
                        mediaPlayer.reset();
                        mediaPlayer.setDataSource(context, uri);
                        mediaPlayer.prepare();
                        mediaPlayer.start();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }


    MediaPlayer.OnCompletionListener completionListener = new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            Log.i("zx", "mediaPlayer onCompletion");
            try {
                Thread.sleep(500);
                execute();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    };
    MediaPlayer.OnErrorListener errorListener = new MediaPlayer.OnErrorListener() {
        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
            Log.i("zx", "mediaPlayer onError");
            mediaPlayer.reset();
            execute();
            return false;
        }
    };

    public void execute() {
        if (queue != null && queue.size() > 0) {
            queue.remove(0);
            if (queue.size() > 0) {
                int nowType = queue.get(0);
                Uri uri = null;
                if (BaseConfigs.VOICE_SCAN_HINT == nowType) { //扫码提示
                    uri = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.scan_hint);
                }else{

                }
                if (uri != null) {
                    try {
//                            vibrator.vibrate(pattern, -1);
                        mediaPlayer.reset();
                        mediaPlayer.setDataSource(context, uri);
                        mediaPlayer.prepare();
                        mediaPlayer.start();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public void destory() {
        if (queue != null)
            queue.clear();
        if (mediaPlayer != null) {
            //资源释放
            mediaPlayer = null;
            mediaPlayer.release();
        }
        isInit = false;
//        if (vibrator != null)
//            vibrator.cancel();
    }
}
